﻿#include "dl_3.h"

TeAiFlowEngineMultiExec multiEngine;
vector<AIDll*> vector_dl_3;
//int model_number = 0;
//int model_number_max = 0;
bool dl_3_init()
{
    /////////////4、将工程添加给多工程对象/////////////
    for(auto obj: vector_dl_3)
        multiEngine.addEngine(&obj->m_engine);
    /////////////5、初始化执行/////////////

    bool  ret = multiEngine.initMultiExec();

    /////////////6、开始执行工程/////////////

    ret = multiEngine.startMultiExec();
    return ret;
}
bool dl_3_end()
{
    /////////////9、结束执行工程/////////////
    bool ret = multiEngine.stopMultiExec();
    /////////////10、释放工程/////////////
    ret = multiEngine.releaseMultiExec();
    return ret;
}

TeImage DeepConvertMat2TeImage(const cv::Mat& mat)

{

    int width = mat.cols;

    int height = mat.rows;

    int channels = mat.channels();

    size_t stepWidth = mat.step1();

    void* data = mat.data;



    return TeImage(width, height, channels, stepWidth, data).clone();

}

bool AIDll::loadPorjectFiles(string path, std::vector<string> node, int image_width, int image_height, int channel,string& message)
{
    std::string error;
    /////////////1、加载工程/////////////
    bool ret = m_engine.loadProj(path);
    if (!ret) 
    {
        error = m_engine.getLastError();
        message = error;
        return false;
    }
    ////////////////////////////////////////
    /////////////2、设置输入节点/////////////
    //
    ret = m_engine.addInputImageNode("image", image_width,  image_height,  channel);
	if (ret == false)
	{
		message = "addInputImageNode error ";
		return false;
	}
    ////////////////////////////////////////
    // 
    /////////////3、设置输出节点/////////////
    m_vector_node.clear();
    m_vector_node.assign(node.begin(),node.end());
    for (auto obj : m_vector_node)
    {
        ret = m_engine.addAiNodeOutput(obj, "defaultFlow");
        m_engine.setOutputBufferEnable(true);
        if (!ret)
        {
            error = m_engine.getLastError();
            message = error;
            return false;
        }
    }
	
//    model_number++;


//    if (model_number ==3)
//    {
//        init(vector_aidll);
//    }
    return true;
}

int AIDll::predictImage(Mat mat_image, vector<vector<AIResult>>& result)
{
    TeImage image = DeepConvertMat2TeImage(mat_image);

    /////////////6、输入数据/////////////
    bool ret = m_engine.pushData("image", image);
    if (!ret)
    {
		
        //std::cout << m_engine.getLastError() << std::endl;
        return -1;
    }
    ////////////////////////////////////////

    /////////////7、获取结果/////////////
    for (auto obj : m_vector_node)
    {
        std::vector<AiInstance> instances;
        ret = m_engine.getAiNodeOutput(obj, "defaultFlow", &instances);
        vector<AIResult> vector_defectresult;
        for (auto obj : instances)
        {
			AIResult tem;
            tem.name = obj.strName;
            tem.threshold_score = obj.fProbit;
			tem.area            = obj.getArea();
			TeRect2f rect       = obj.getBoundingRect();
			tem.width           = rect.fWidth;
			tem.height          = rect.fHeight;
			tem.x               = rect.fX + rect.fWidth/2;
			tem.y               = rect.fY + rect.fHeight / 2;
			rect.fWidth;
            for (auto obj_contour1 : obj.vvContour)
            {
                std::vector<cv::Point> vector_point2f;
                for (auto obj_contour2 : obj_contour1)
                    vector_point2f.push_back(Point(obj_contour2.fX, obj_contour2.fY));
                tem.contour.push_back(vector_point2f);
            }
            vector_defectresult.push_back(tem);
        }
        result.push_back(vector_defectresult);
    }
    return 1;
}

//bool AIDll::releaseProjectFiles()
//{
//    model_number--;
//    if (model_number == 0)
//    {
//        end(vector_aidll);
//    }
//}

//AlgrithmInterface* newInstance()
//{
//    AIDll* instance = new AIDll();
//    vector_aidll.push_back(instance);
//    return static_cast<AlgrithmInterface*>(instance);
//}
